package org.zoomdev.zoom.rpc.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class ClientIdleHandler extends ChannelInboundHandlerAdapter {
    private ClientConnection connection;

    public ClientIdleHandler() {
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.ALL_IDLE) {
                connection.heartBeat();
                return;
            }
        }

        super.userEventTriggered(ctx, evt);
    }

    public void setConnection(ClientConnection connection) {
        this.connection = connection;
    }


}
